//
//  String+Ex.swift
//  APP
//
//  Created by Rdxer on 2017/12/14.
//  Copyright © 2017年 Rdxer. All rights reserved.
//

import Foundation

public extension String{
    /// 返回 长度 > 0 的字符串或者 nil
    var nilOrNotEmptyString:String?{
        return self.isEmpty ? nil : self
    }
    
    static func hasValue(value:String?)->Bool{
        if let v = value?.nilOrNotEmptyString,v.length > 0 {
            return true
        }
        return false
    }
    static func eq(value:String?,value2:String?)->Bool{
        guard let v1 = value?.nilOrNotEmptyString ,
              let v2 = value2?.nilOrNotEmptyString
        else { return false }
        
        if v1 == v2 {
            return true
        }
        
        return false
    }
    
}

public extension Substring{
    /// 返回 长度 > 0 的字符串或者 nil
    var nilOrNotEmptyString:String?{
        return self.isEmpty ? nil : String(self)
    }
}

public extension String{
    /// 安全的取数组范围的 字符串  开区间:   [fromIndex,toIndex)
    func cut(from fromIndex:Int = -1,to toIndex:Int = Int.max)->String{
        
        if count == 0 {
            return ""
        }
        
        var res:String = ""
        
        for index in 0..<self.count {
            if (index > fromIndex && index < toIndex) || index == fromIndex{
                let strIndex = self.index(self.startIndex, offsetBy: index)
                let c = self[strIndex]
                res.append(c)
            }
        }
        
        return res
    }
}

public extension String{
    /*
     let str = "1234412321321"
     if let index = str.findSubstr(target: "132"){
     log.e(index.description)
     log.e(str.cut(from: index,to:"132".length + index))
     }else{
     log.e("no found")
     }
     */
    /// 查找子字符串
    ///
    public func findSubstr(target:String,from fromIndex:Int = -1,to toIndex:Int = Int.max)->Int?{
        
        if target.isEmpty {
            return nil
        }
        
        if target.length > length {
            return nil
        }
        
        if target.length == length {
            if target == self {
                return 0
            }
            return nil
        }
        
        if count == 0 {
            return nil
        }
        
        let sublen = target.length
        
        // 判断
        for index in 0..<self.count {
            if index < fromIndex || index > toIndex{
                continue
            }
            
            var isEq = true
            for offset in 0..<sublen{
                if sublen + index >= length{
                    return nil
                }
                if target.asNSString.character(at:offset) != self.asNSString.character(at: index + offset){
                    isEq = false;
                    break
                }
            }
            if isEq {
                return index
            }
        }
        
        return nil
    }
}

public extension String{
    /// 返回 nsstring
    var asNSString:NSString{
        return self as NSString
    }
    
}
